from sqlalchemy import Column, Integer, String, DateTime
from sqlalchemy.orm.session import Session

from airflow.models.base import Base
from airflow.models.idata_project_datasource import ProjectDatasource
from airflow.models.idata_project_user import ProjectUser
from airflow.utils.session import provide_session


class Project(Base):
    """the class to get user info."""

    __tablename__ = "idata_project"
    __table_args__ = {"useexisting": True}

    id = Column(Integer, primary_key=True, autoincrement=True)
    project_name = Column(String(128))
    project_desc = Column(String(255))
    pool = Column(String(64))
    project_status = Column(Integer)
    create_by = Column(String(32))
    create_time = Column(DateTime)
    enable = Column(Integer)

    def __init__(self, project_name, project_desc, project_status, oa_user_name, create_time, pool):
        self.project_name = project_name
        self.project_desc = project_desc
        self.project_status = project_status
        self.create_by = oa_user_name
        self.create_time = create_time
        self.pool = pool
        self.enable = 0

    @staticmethod  # 静态方法
    @provide_session
    def get_project(id, session: Session = None):
        return session.query(Project).filter(Project.id == id).first()

    @staticmethod  # 静态方法
    @provide_session
    def get_projects(session: Session = None):
        return session.query(Project).all()

    @staticmethod  # 静态方法
    @provide_session
    def get_projects_by_oa_user_name_page(oa_user_name, page, session: Session = None):
        return session.query(Project).outerjoin(ProjectUser, Project.id == ProjectUser.project_id) \
            .filter(ProjectUser.oa_user_name == oa_user_name).paginate(page, 10, error_out=False)

    @staticmethod  # 静态方法
    @provide_session
    def get_projects_by_oa_user_name(oa_user_name, session: Session = None):
        return session.query(Project).outerjoin(ProjectUser, Project.id == ProjectUser.project_id) \
            .filter(ProjectUser.oa_user_name == oa_user_name)

    @staticmethod  # 静态方法
    @provide_session
    def get_projects_page(page, session: Session = None):
        return session.query(Project).order_by(Project.create_time).paginate(page, 10, error_out=False)

    @staticmethod
    @provide_session
    def add_project(project, session: Session = None):
        return session.add(project)

    @staticmethod
    @provide_session
    def delete_project(id, session: Session = None):
        """
        delete user role  by id .

        :param id: The role_resource id of the roleResource to get.
        :param session: SQLAlchemy ORM Session
        """
        session.query(Project).filter(Project.id == id).delete()

    @staticmethod
    @provide_session
    def update_project(project, session: Session = None):
        result = session.query(Project).filter(Project.id == project.id).first()
        if result is None:
            return
        else:
            return session.merge(project)

    @staticmethod  # 静态方法
    @provide_session
    def get_datasource_project(datasource_id, session: Session = None):
        return session.query(Project).outerjoin(ProjectDatasource, Project.id == ProjectDatasource.project_id).filter(ProjectDatasource.datasource_id == datasource_id).all()

    def to_json(self):
        if self is None:
            return None
        data = {
            'id': self.id,
            'project_name': self.project_name,
            'project_desc': self.project_desc,
            'pool': self.pool,
            'project_status': self.project_status,
            'create_by': self.create_by,
            'create_time': self.create_time,
            'enable': self.enable,
        }
        return data
